//
//  STSpreadCityListViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/24.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "STSpreadOrderDetailViewModel.h"

#import "SpreadOrderDetailData.h"

@interface STSpreadOrderDetailViewModel ()

@property (nonatomic, readwrite, assign) BOOL isHavePay;
@property (nonatomic, readwrite, copy) NSString* payTypeLabel;
@property (nonatomic, readwrite, copy) NSString* citysLabel;
@property (nonatomic, readwrite, copy) NSString* dateLabel;
@property (nonatomic, readwrite, copy) NSString* totalPriceLabel;
@property (nonatomic, readwrite, copy) NSString* discount4CoinLabel;
@property (nonatomic, readwrite, copy) NSString* discountLabel;
@property (nonatomic, readwrite, copy) NSString* discountPriceLabel;
@property (nonatomic, readwrite, copy) NSString* finalPayLabel;
@property (nonatomic, readwrite, copy) NSString* orderNumLabel;
@property (nonatomic, readwrite, copy) NSString* orderPayTimeLabel;

@property (nonatomic, readwrite, strong) SpreadOrderDetailData* detailData;

@property (nonatomic, readwrite, strong) RACCommand* reqDetailCmd;
@property (nonatomic, readwrite, strong) RACCommand* payCmd;

@end

@implementation STSpreadOrderDetailViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    self.reqDetailCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
        
        return [self _requestOrderInfoSignal];
    }];
    
    self.payCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
                   {
                       @strongify(self);
                       
                       @weakify(self);
                       return [self _requestOrderSignal];
                   }];
}

- (RACSignal*)_requestOrderSignal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    
    NSMutableArray* strAreas = [NSMutableArray array];
    for (AreaInfoData* curAreaInfoData in self.detailData.area)
    {
        [strAreas addObject:curAreaInfoData.areaID];
    }
    NSString* strArea = [strAreas componentsJoinedByString:@","];
    NSString* strTime = self.detailData.time;
    NSString* strIsCharge = @"1";
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);
                
                @weakify(self);
                [[AFNetWorkUtils racPOSTWthURL:url_interface_spread_submit params:interface_param_spread_submit(um.userData.userID, strArea, strTime, strIsCharge)] subscribeNext:^(id x)
                 {
                     @strongify(self);
                     
                     [subscriber sendNext:x];
                     [subscriber sendCompleted];
                 } error:^(NSError *error)
                 {
                     [subscriber sendCompleted];
                 }];
                
                return nil;
            }];
}

- (RACSignal*)_requestOrderInfoSignal
{
    @weakify(self);
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
    {
        @strongify(self);
        
        NSDictionary* postData = interface_param_spread_spreaddetail(self.spreadID);
        
        [[AFNetWorkUtils racPOSTWthURL:url_interface_spread_spreaddetail params:postData] subscribeNext:^(id x)
        {
         
            SpreadOrderDetailData* data = [SpreadOrderDetailData modelWithJSON:x];
            self.detailData = data;
            
            self.isHavePay = [data.status isEqualToString:@"1"];
            
            self.payTypeLabel = [data.status isEqualToString:@"1"] ? @"待支付" : ([data.status isEqualToString:@"2"] ? @"已支付" : @"退款"); //1待支付2已支付3退款
            
            NSMutableArray* citys = [NSMutableArray array];
            for (NSInteger index=0; index<data.area.count; index++)
            {
                AreaInfoData* areaInfoData = data.area[index];
                
                [citys addObject:areaInfoData.areaName];
            }
            self.citysLabel = [citys componentsJoinedByString:@"、"];
            
            self.dateLabel = [data.time isEqualToString:@"1"] ? @"三个月" : ([data.time isEqualToString:@"2"] ? @"六个月" : ([data.time isEqualToString:@"3"] ? @"一年" : @"两年")); //1三个月2六个月3一年4两年
            self.dateLabel = HCStrFormat(@"推广月份：%@", self.dateLabel);
            
            NSString* totalPrice = [NSString decimalwithFormat:@"0.00" floatV:[data.primeCost floatValue]];
            self.totalPriceLabel = HCStrFormat(@"¥%@", totalPrice);
            
            self.discount4CoinLabel = HCStrFormat(@"%d个生态币", (int)[data.offsetMoney intValue]);
            
            self.discountLabel = HCStrFormat(@"折扣（%@折）", data.discount);
            
            NSString* discountPrice = [NSString decimalwithFormat:@"0.00" floatV:[data.discountPrice floatValue]];
            self.discountPriceLabel = HCStrFormat(@"-¥%@", discountPrice);
            
            NSString* finalPrice = [NSString decimalwithFormat:@"0.00" floatV:[data.price floatValue]];
            self.finalPayLabel = HCStrFormat(@"¥%@", finalPrice);
            
            self.orderNumLabel = HCStrFormat(@"订单号：%@", data.spreadSN);
            
            self.orderPayTimeLabel = HCStrFormat(@"付款时间：%@", data.payTime);
            
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
            
        } error:^(NSError *error)
        {
            //错误处理
            
            [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
}

@end
